package 爆刷leetcode.balabala.移除重复元素;

/**
 * @Author yaya
 * @Create 2024/5/25 11:43
 * Description:
 */
public class RemoveElement {
    public static void main(String[] args) {
        int i = removeElement(new int[]{4,5}, 5);
        System.out.println(i);
    }

    public static int removeElement(int[] nums, int val) {
        // 左右指针
        int left = 0;
        int right = nums.length - 1;
        int temp;
        int count = 0;

        if (nums.length == 1) {
            return nums[0] == val ? 0 : 1;
        }


        while (left <= right) {
            if (nums[left] == val) {
                while (nums[right] == val && left < right) {
                    right--;
                    count++;
                }

                if (left > right) break;
                temp = nums[left];
                nums[left] = nums[right];
                nums[right] = temp;
                right--;
                count++;
            }
            left++;
        }

        return nums.length - count;
    }
}


